package com.pqtest.api.service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.pqtest.api.dto.SlSimulibraryAnswerDto;
import com.pqtest.api.entity.*;
import com.pqtest.api.mapper.SlSimuTestMapper;
import com.pqtest.api.mapper.SllibraryMapper;
import com.pqtest.api.mapper.TestCategoryMapper;
import com.pqtest.api.utils.DtoEntityTransformer;
import io.swagger.models.auth.In;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Log
public class SllibraryService {
    @Autowired
    SllibraryMapper sllibraryMapper;
    @Autowired
    TestCategoryMapper testCategoryMapper;
    @Autowired
    SlSimuTestMapper slSimuTestMapper;
    @Autowired
    UserService userService;
    public List<SllibraryEntity>  getList(int uid,String reqStart,String reqLimit){
        UserEntity userEntity = userService.getById(uid);
        TestCategoryEntity testCategoryEntity = testCategoryMapper.getTestCategoryById((long)userEntity.getPqTestCategoryId());
        int rs = 0,rl = 20;
        if(reqStart != null && !reqStart.equals("")){
            rs = Integer.parseInt(reqStart);
        }
        if(reqLimit != null && !reqLimit.equals("")){
            rl = Integer.parseInt(reqLimit);
        }
        PageHelper.offsetPage(rs > 0 ? rs : 0, rl > 0 ? rl : 20);
        return sllibraryMapper.getListByTestCategory(testCategoryEntity);
    }

    public long makeTraining(int uid,int libraryId){
        SlSimulibraryEntity slSimulibraryEntity = new SlSimulibraryEntity();
        slSimulibraryEntity.setUserId(uid);
        slSimulibraryEntity.setLibraryId(libraryId);
        sllibraryMapper.training(slSimulibraryEntity);
        return slSimulibraryEntity.getId();
    }

//    public List<SlSimulibraryMaterialEntity>getMaterial(int simuId){
//        return sllibraryMapper.getMaterialById(simuId);
//    }

    public List<SlSimulibraryQuestionEntity>getQuestion(int simuId) throws Exception {
        SlSimulibraryEntity slSimulibraryEntity = slSimuTestMapper.getById(simuId);
        if(slSimulibraryEntity == null){
            throw new Exception("no record");
        }
        return sllibraryMapper.getQuestionById(slSimulibraryEntity.getLibraryId(),simuId);
    }

    public JSONObject getMaterialAndQuestion(int simuId) throws Exception {
        SlSimulibraryEntity slSimulibraryEntity = slSimuTestMapper.getById(simuId);
        if(slSimulibraryEntity == null){
            throw new Exception("no record");
        }
        JSONObject o = new JSONObject();
        o.put("materials",sllibraryMapper.getMaterialById(slSimulibraryEntity.getLibraryId()));
        o.put("questions",sllibraryMapper.getQuestionById(slSimulibraryEntity.getLibraryId(),simuId));
        return o;
    }


    public long answer(SlSimulibraryAnswerDto slSimulibraryAnswerDto){
        SlSimulibraryAnswerEntity slSimulibraryAnswerEntity = new SlSimulibraryAnswerEntity();
        DtoEntityTransformer.copyProperties(slSimulibraryAnswerDto,slSimulibraryAnswerEntity);
        sllibraryMapper.answer(slSimulibraryAnswerEntity);
        return slSimulibraryAnswerEntity.getId();
    }
}
